import datetime


class UserData (): #UserData

    def __init__(self) -> None:
        self.anniversary = None
        self.UserList = [{},{}]
        self.__action_Exp = [1,2,3,4,5,6]
        self.level_Exp = [0,10,30,60,100,150,210,280,360,450]
        self.items = [{},{}]
        self.lock = 0
        self.last = [None,None]

        dict = {'Identity':0,'Birthday':None,'Age':0,'Password':'','Exp':0,'Level':1,\
        'Nickname':'','Activated':0}

        self.UserList[0] = dict
        
        dict = {'Identity':1,'Birthday':None,'Age':0,'Password':'','Exp':0,'Level':1,\
        'Nickname':'','Activated':0}

        self.UserList[1] = dict

        dict1 = {'Style':1,'Key':0}
        dict2 = {'Style':1,'Key':0}
        self.items[0] = dict1
        self.items[1] = dict2


    def LevelUp (self,action,identity):
        level = self.UserList[identity]['Level']
        experience = self.UserList[identity]['Exp']
        levelup = 0

        if level >= 10: #高于10级不再获得经验
            return

        experience += self.__action_Exp[action]

        while level < 10 and self.level_Exp[level] <= experience:
            level += 1
            levelup = 1

        self.UserList[identity]['Level'] = level
        experience = self.UserList[identity]['Exp'] = experience

        if levelup != 1: #未升级
            return

        if self.UserList[identity]['Level'] == 3:
            self.lock = 4096
        elif self.UserList[identity]['Level'] == 4:
            self.items[identity]['Style'] = 2
        elif self.UserList[identity]['Level'] == 6:
            self.items[identity]['Style'] = 3
        elif self.UserList[identity]['Level'] == 7:
            self.lock = 2048
        elif self.UserList[identity]['Level'] == 8:
            self.items[identity]['Style'] = 4
        elif self.UserList[identity]['Level'] == 9:
            self.lock = 1024
        elif self.UserList[identity]['Level'] == 10:
            self.items[identity]['Key'] += 1
            self.items[identity]['Style'] = 6
            self.last[identity] = datetime.date.today ()


    def SetAnniversary (self,year,month,day): #设置纪念日
        self.anniversary = datetime.date(year,month,day)

    
    def __AgeCpt(self,birthday): #计算用户年龄
        today = datetime.date.today()

        year = today.year
        month = today.month
        day = today.day
        age = year - birthday.year

        if month < birthday.month:
            return age-1

        elif month == birthday.month:
            if day < birthday.day:
                return age-1
            else:
                return age

        else:
            return age

    
    def __Encryption(self,password,birthday): #凯撒密码加密
        en_password = ""
        en_num = (birthday.year + birthday.month + birthday.day) % 3 + 1 #设置密码偏移量

        for c in password: #按序加密每个字符
            i = ord(c) + en_num
            n = chr(i)
            en_password = en_password + n
        return en_password


    def __Decryption(self,en_password,birthday): #凯撒密码解密
        password = ""
        en_num = (birthday.year + birthday.month + birthday.day) % 3 + 1 #设置密码偏移量

        for c in en_password: #按序解密每个字符
            i = ord(c) - en_num 
            n = chr(i)
            password = password + n
        return password


    def Register (self,identity,birthday,password,nickname): #注册

        if self.UserList[identity]['Activated'] == 1:
            return 0 #已经注册

        age = self.__AgeCpt(birthday)
        en_password = self.__Encryption(password,birthday)

        new_list = {'Identity':identity,'Birthday':birthday,'Age':age,'Password':en_password\
        ,'Exp':0,'Level':1,'Nickname':nickname,'Activated':1}
        self.UserList[identity] = new_list

        return 1 #注册成功

    
    def Login (self,identity,password): #身份登录
        if self.UserList[identity]['Activated'] == 0:
            return -1 #身份未注册

        en_password = self.__Encryption(password,self.UserList[identity]['Birthday'])

        if self.UserList[identity]['Password'] == en_password:
            return 1 #成功登陆
        else:
            return 0 #密码错误
    

    def PasswordChange(self,identity,ori_password,new_password): #修改密码
        if self.UserList[identity]['Activated'] == 0:
            return -1 #身份未注册

        en_password = self.__Encryption(ori_password,self.UserList[identity]['Birthday'])

        if self.UserList[identity]['Password'] == en_password:
            en_password = self.__Encryption(new_password,self.UserList[identity]['Birthday'])
            self.UserList[identity]['Password'] = en_password
            return 1 #成功

        else:
            return 0 #原密码错误

    
    def DayCount (self): #计算恋爱天数
        today = datetime.date.today()
        count = (today - self.anniversary).days
        return count